package com.test.rxjavademo;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class MapDemo {

    public void getStudentInfo(){
        StudentModel.init();

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                List<Student> studentList = StudentModel.getStudentList();
//                for (Student student : studentList) {
//                    List<Course> courseList = student.getCourseList();
//                    for(Course course : courseList){
//                        System.out.println(student);
//                        System.out.println(course);
//                    }
//                }
//            }
//        });


//        Observable.fromIterable(StudentModel.getStudentList()).
//                map(new Function<Student, List<Course>>() {  //对事件进行处理，
//
//                    @Override
//                    public List<Course> apply(Student student) throws Exception {
//                        return student.getCourseList();
//                    }
//                }).subscribe(new Consumer<List<Course>>() {
//                    @Override
//                    public void accept(List<Course> courses) throws Exception {
//                        for(Course course : courses){
//                            System.out.println(course);
//                        }
//                    }
//                });

        Observable.fromIterable(StudentModel.getStudentList())
                .flatMap(new Function<Student, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(Student student) throws Exception {
                        return Observable.fromIterable(student.getCourseList());
                    }
                }).subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        System.out.println(o);
                    }
                });
    }

}
